"""
Created on 29-Oct-2009

@author: dexen
"""

import os
import time
import thread
import threading
import rpyc

import multiprocessing as mp
from multiprocessing.managers import BaseManager

"""
class MathsClass(object):
    def __init__(self):
        self.dd = {}
        self.i = 0
        self.lock = threading.Lock()
        
    def add(self, a):
        self.dd[self.i] = a
        self.dd[self.i]["haha"] = 9
        self.i += 1
        print "in add", thread.get_ident() 
        print self.dd

    def tt(self):
        print "in tt", thread.get_ident(), os.getpid()



class MyManager(BaseManager):
    pass

MyManager.register("Maths", MathsClass)
"""

class LocalClass(object):
    def __init__(self):
        self.inq = mp.Queue()

    def put(self, item):
        self.inq.put(item)

    def get(self):
        while self.inq.qsize() == 0:
            pass
        item = self.inq.get()
        return item    


class Unit(object):
    def __init__(self):
        self.x = 5

def test(obj):
    print "in test"
    print thread.get_ident()
    print os.getpid()
    time.sleep(5)
    while True:
        print obj.inq.qsize()
        item = obj.inq.get_nowait()
        #print item
        time.sleep(1)
        #obj.add({"y":3})
        
    

def test1():
    #manager = MyManager()
    #manager.start()
    #maths = manager.Maths()

    maths = LocalClass()
    p = mp.Process(target=test, args=(maths,))
    p.start()

    maths.put(Unit())
    
    #time.sleep(5)
    
    print "in main", thread.get_ident(), os.getpid()
    
    #while True:
    #    time.sleep(1)
    #    maths.put(time.time())

    print "I am dead"
    
    """
    while True:
        time.sleep(1)
        maths.add({"x":4})
    """ 

def kk():
    """
    time.sleep(6)
    return
    while True:
        pass
    """
    while True:
        pass
    q = mp.Queue()
    q.get_nowait()

def ll():
    p = mp.Process(target=kk)
    p.start()
    
    """
    time.sleep(1)
    q = mp.Queue()
    q.get_nowait()
    """
    while True:
        pass
    
def test2():   
    p = mp.Process(target=ll)
    #p.daemon = True
    print "is daemon", p.daemon
    p.start()

def main():
    #test1()  
    test2()
    time.sleep(1)
    raise Exception()
    while True:
        pass

if __name__ == "__main__":
    main()
